<?php
session_start();
if (!isset($_SESSION['admin']) or !$_SESSION['admin']) {  //说明未登录
    echo "<script>alert('本页面需要管理员登录后才能访问！');location.href='index.php';</script>";
    exit;
}
include_once "conn.php";
$sql = "select * from userinfo ";
$result = mysqli_query($conn, $sql);
include_once "page.php";
$count_rows = mysqli_num_rows($result);
//引用分页函数
$perPage = 10;
pageft($count_rows, $perPage, $count_rows);
$sql .= " limit $firstcount, $displaypg";
$result = mysqli_query($conn, $sql);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="css/demo.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <link rel="stylesheet" type="text/css" href="css/animate-custom.css"/>
    <title>会员列表</title>
    <style>
        tr {
            width: 100%;
            border: 1px solid rgba(147, 184, 189, 0.8);
            border-radius: 5px;
            box-shadow: 0pt 2px 5px rgba(105, 108, 109, 0.7), 0px 0px 8px 5px rgba(208, 223, 226, 0.4) inset;
        }

        table {
            position: relative;
            top: 20px;
        }

        td:hover {
            border-collapse: collapse;
            transition: transform 0.4s;
            transform: translatey(-3px);
        }

        td {
            width: 8.5%;
            padding: 8px;
        }

        td:last-child {
            width: 50%;
        }

        li {
            float: left;
            width: 8%;
            position: relative;
            left: 38%;
            font-size: 20px;
            padding-top: 1%;
        }

        #wrapper {
            right: 0;
            min-height: 560px;
            margin: 0 auto;
            width: 1000px;
            position: relative;
        }

        #a {
            max-width: 80px;
            max-height: 80px;
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1><span>会员列表</span></h1>
        <nav class="codrops-demos">
            <?php
            if (isset($_SESSION['loggedUsername']) && $_SESSION['loggedUsername']) {
                ?>
                <h2>欢迎管理员<strong><?php echo $_SESSION['loggedUsername'] ?></strong></h2>
                <?php
            }
            ?>
        </nav>
        <nav>
            <ul>
                <li><a href="index.php"><strong>首页</strong></a></li>
                <li><a href="log.php"> <strong>会员登录日志</strong></a></li>
                <li><a href="zhuxiao.php"> <strong>注销</strong></a></li>
            </ul>
        </nav>
    </header>
    <section>
        <div id="container_demo">
            <div id="wrapper">
                <div id="login" class="animate form">
                    <table align="center" style="border-collapse: collapse" border="1" bordercolor="#999999">
                        <tr>
                            <td>序号</td>
                            <td>用户名</td>
                            <td>性别</td>
                            <td>信箱</td>
                            <td>年龄</td>
                            <td>爱好</td>
                            <td>个人介绍</td>
                            <td>用户头像</td>
                            <td>是否管理员</td>
                            <td>操作</td>
                        </tr>
                        <?php
                        //$i = 1;
                        $page = isset($_GET['page']) ? $_GET['page'] : 1;
                        $i = ($page - 1) * $perPage + 1;
                        while ($info = mysqli_fetch_array($result)) {
                            ?>
                            <tr>
                                <td><?php echo $i ?></td>
                                <td><?php echo $info['username']; ?></td>
                                <td><?php echo $info['sex']; ?></td>
                                <td><?php echo $info['email']; ?></td>
                                <td><?php echo $info['age']; ?></td>
                                <td><?php echo $info['fav']; ?></td>
                                <td><?php echo $info['ren']; ?></td>
                                <td><?php if ($info['pic']) { ?><img id="a"
                                                                     src="upload/<?php echo $info['pic'] ?>"><?php } else {
                                        echo "暂无头像";
                                    } ?></td>
                                <td><?php echo $info['admin'] ? '是' : '否'; ?></td>
                                <td>
                                    <?php
                                    if ($_SESSION['loggedUsername'] == 'admin') { //只有admin可以设置或取消管理员
                                        if ($info['username'] == 'admin') {  //不能修改自己的状态
                                            echo "<span style=\"text-decoration: line-through;color: gray\">改变管理员</span>";
                                        } else { //只能修改其他人的状态
                                            if ($info['admin']) {
                                                echo "<a href='setAdmin.php?id=" . $info['id'] . "&action=0'>取消管理员</a>";
                                            } else {
                                                echo "<a href='setAdmin.php?id=" . $info['id'] . "&action=1'>设置管理员</a>";
                                            }
                                        }
                                    } else {
                                        echo "<span style=\"text-decoration: line-through;color: gray\">改变管理员</span>";
                                    }
                                    ?>
                                    <?php
                                    if ($_SESSION['loggedUsername'] == 'admin') {//先判断当前用户是不是admin。如果是，则设置条件为：不删除自己即可。
                                        if ($info['username'] != 'admin') { //如果不是自己，则显示带链接的删除
                                            echo "<a href='del.php?id=" . $info['id'] . "'>删除</a>";
                                        } else { //说明是自己，则显示带删除线的删除
                                            echo "<span style=\"text-decoration: line-through;color: gray\">删除</span>";
                                        }
                                    } else {//如果是普通管理员，则不删除管理员即可。
                                        if ($info['admin']) { //说明是管理员，则显示带删除线的删除
                                            echo "<span style=\"text-decoration: line-through;color: gray\">删除</span>";
                                        } else { //说明不是管理员，则显示带链接的删除
                                            echo "<a href='del.php?id=" . $info['id'] . "'>删除</a>";
                                        }
                                    } ?></td>
                            </tr>
                            <?php
                            $i++;
                        }
                        ?>
                    </table>
                    <table>
                        <tr>
                            <td align="right">
                                <?php
                                //页尾显示
                                echo $pagenav;
                                ?>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </section>
    <div style="text-align:center;clear:both;position: relative;top: 30%">
        <p> 版权归第三组所有；使用本网站须陈杰同意，违者必究。 </p><br>
        <p>©2018-2020 陈杰,&nbsp&nbsp&nbsp蓝跃，黎大王</p>
    </div>
</div>
</body>
</html>
